package com.ruix.app.ui.stratum.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.mapper.ActionMapper;
import com.ruix.app.model.Classes;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.base.BaseActivity;
import com.ruix.app.util.SoftInputUtil;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/4/25.
 */

public class PushAdviceActivity extends BaseActivity{


    public static Intent newIntent(Context context, Classes classes){
        Intent intent = new Intent(context,PushAdviceActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("classes",classes);
        intent.putExtras(bundle);
        return intent;
    }

    private  EditText adviceContentEdit;
    private Classes currentClasses;

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_feadback_advice;
    }

    @Override
    protected void initView() {
        setTitle("意见反馈");
        adviceContentEdit =  (EditText)findViewById(R.id.ev_advice_content);
        adviceContentEdit.setImeOptions(EditorInfo.IME_ACTION_DONE);
        adviceContentEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    SoftInputUtil.hideSoftInput(adviceContentEdit);
                    submit();
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    protected void initLocalData() {
        Intent intent = getIntent();
        if (intent != null){
            currentClasses =  (Classes)intent.getSerializableExtra("classes");
        }
    }

    @Override
    protected void initListener() {
        findViewById(R.id.sbumit_advice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    public void submit(){
        String content = adviceContentEdit.getText().toString().trim();
        if (TextUtils.isEmpty(content)){
            showErrorTip("请输入反馈意见");
            return;
        }
        if (isContentMoreThreeHundred(content)){
            showErrorTip("字数限制300字");//反馈意见不能超过300个字符
            return;
        }

        showLoading();
        RequestFactory.feedbackAdvice(currentClasses.getID(), content, new ActionMapper(), new RXObserver<Boolean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Boolean value) {
                if (value){
                    showSuccessTip("反馈成功");
                    dismissLoading();
                    finish();
                }
            }

            @Override
            public void onError(Throwable e) {
                dismissLoading();
                resolveException(e);
            }

            @Override
            public void onComplete() {

            }
        });

    }

    private boolean isContentMoreThreeHundred(String content){
        if (content.length() > 150){
            String newString = content.replaceAll("[[\u4e00-\u9fa5]]", "aa");
            if (newString.length() > 300) {
                return true;
            }
        }
        return false;
    }
}
